package com.lanbu.web.controller.camera;

import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class PushService {

    private static PushApi pushApi;

    public void setPushApi(PushApi pushApi) {
        PushService.pushApi = pushApi;
    }


    public void pushMessage(String title, String content, String payload, String cid) {
        //根据cid进行单推

        Settings settings = new Settings();
        settings.setTtl(3600000);

        //在线走个推通道时推送的消息体
        PushMessage pushMessage = new PushMessage();
        pushMessage.setTransmission("{title:\"" + title + "\",content:\"" + content + "\",payload:\"" + payload + "\"}");

        // 设置接收人信息
        Audience audience = new Audience();
        audience.addCid(cid);

        //设置离线推送时的消息体
        PushChannel pushChannel = new PushChannel();
        //安卓离线厂商通道推送的消息体
        AndroidDTO androidDTO = new AndroidDTO();
        Ups ups = new Ups();
        ThirdNotification thirdNotification = new ThirdNotification();
        thirdNotification.setTitle("离线" + title);
        thirdNotification.setBody("离线" + content);
        thirdNotification.setClickType("intent");
        thirdNotification.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;package=net.csdn.blog;component=uni.UNIF074E9E/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title="+title+";S.content="+content+";S.payload=" + payload + ";end");
        ups.setNotification(thirdNotification);
        androidDTO.setUps(ups);
        pushChannel.setAndroid(androidDTO);

        //ios离线apn通道推送的消息体
        Alert alert = new Alert();
        alert.setTitle("离线" + title);
        alert.setBody("离线" + content);
        Aps aps = new Aps();
        aps.setContentAvailable(0);
        aps.setSound("default");
        aps.setAlert(alert);
        IosDTO iosDTO = new IosDTO();
        iosDTO.setAps(aps);
        iosDTO.setType("notify");
        pushChannel.setIos(iosDTO);


        //设置推送消息体
        PushDTO<Audience> pushDTO = new PushDTO<>();
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        pushDTO.setSettings(settings);
        pushDTO.setPushMessage(pushMessage);
        pushDTO.setPushChannel(pushChannel);
        pushDTO.setAudience(audience);

        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            System.out.println("cid: " + cid + ", code: " + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        } else {
            System.out.println("cid: " + cid + ", code: " + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }
}
